# utils/request_utils.py
import requests
import json


class RequestUtils:
    """请求工具类 - utils技术点"""

    @staticmethod
    def post_request(url, data=None, headers=None):
        """POST请求工具方法"""
        if headers is None:
            headers = {"Content-Type": "application/json"}

        try:
            response = requests.post(url, json=data, headers=headers, timeout=10)
            return response
        except requests.exceptions.Timeout:
            raise Exception("请求超时")
        except Exception as e:
            raise Exception(f"请求失败: {str(e)}")

    @staticmethod
    def check_response(response, expected_status=None, expected_message=None):
        """响应检查工具方法"""
        assert response.status_code == 200, f"HTTP状态码错误: {response.status_code}"

        result = response.json()

        if expected_status:
            assert result["status"] == expected_status, f"状态码不匹配: {result['status']}"

        if expected_message:
            assert expected_message in result["message"], f"消息不匹配: {result['message']}"

        return result